﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Nizm0.QOT.Library.Objects
{
    [XmlRoot("ProcedureInfoHistory")]
    public class ProcedureInfoHistory :IProcedureInfo, IExportable, IXmlSerializable
    {
        public ProcedureInfoHistory()
        { }

        public ProcedureInfoHistory(IProcedureInfo procedure)
        {
            this.Name = procedure.Name;
        }

        private List<ProcedureVersion> _knownVersionList = new List<ProcedureVersion>();

        public List<ProcedureVersion> KnownVersionList
        {
            get
            {
                return _knownVersionList;
            }
            set
            {
                _knownVersionList = value;
            }
        }
        [XmlAttribute("Name")]
        public string Name
        {
            get;
            set;
        }

        private bool _exported = false;
        [XmlAttribute("Exported")]
        public bool Exported
        {
            get {return _exported;}
            set { _exported = value; }
        }

        [XmlIgnore()]
        public ProcedureHistory ProcedureHistory
        {
            get;
            set;
        }

        private List<Query> _queryList = new List<Query>();

        public Query AddQuery(Query query)
        {
            if (!_queryList.Exists(q => q.QueryString == query.QueryString))
            {
                _queryList.Add(query);
                return null;
            }
            else
                return _queryList.Find(q => q.QueryString == query.QueryString);
        }

        public IProcedureInfo GetProcedure()
        {
            throw new NotImplementedException();
        }

        #region Serialization
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            this.Name = reader.GetAttribute("Name");
            if (reader.GetAttribute("Exported") != null)
            {
                this.Exported = bool.Parse(reader.GetAttribute("Exported"));
            }

        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteAttributeString("Name", this.Name);
            writer.WriteAttributeString("Exported", this.Exported.ToString());
        }

        public virtual T Deserialize<T>(string xmlobject)
                    where T : class
        {
            byte[] byteArray = Encoding.ASCII.GetBytes(xmlobject);
            MemoryStream stream = new MemoryStream(byteArray);
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));
            T ip = (T)x.Deserialize(stream);
            return ip;
        }

        public virtual string Serialize()
        {
            using (Stream str = new MemoryStream())
            {
                //System.Runtime.Serialization.Formatters.Binary.BinaryFormatter t = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(ProcedureInfo));
                x.Serialize(str, this);
                using (StreamReader stmr = new StreamReader(str))
                {
                    return stmr.ReadToEnd();
                }
            }
        }
        #endregion

        public override string ToString()
        {
            return string.Format("{0}",this.Name);
        }

        public void Start()
        {
            throw new NotImplementedException();
        }

        public string Identification
        {
            get
            {
                return Name;
            }
            set
            {
                Name = value;
            }
        }
    }
}
